package com.wawayaya.bookreader.stroke.provider;

import android.content.res.AssetManager;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Strokeutils {
    public static final String TAG = Strokeutils.class.getSimpleName();
    public static final int BUF_SIZE = 1024 * 100;

    /** to do copy file only
     *
     * @param strCopyTo target filename
     * @param strCopyFrom source filename
     * @return boolean true, if succeed; false, if fail.
     */
    public static boolean copyFile(String strCopyTo, String strCopyFrom)
    {
        FileChannel inC = null;
        FileChannel outC = null;
        int length = 1048891;
        boolean bSucc = true;

        try{
            FileInputStream in = new FileInputStream(strCopyFrom);
            File file = new File( strCopyTo );
            if( file.exists() )
                file.delete();
            FileOutputStream out = new FileOutputStream(strCopyTo);
            inC = in.getChannel();
            outC = out.getChannel();
            ByteBuffer b = null;
            while( inC.position() < inC.size() )
            {
                if( ( inC.size() - inC.position() ) < length)
                    length = (int)(inC.size() - inC.position());
                else
                    length = 1048891;
                b = ByteBuffer.allocateDirect(length);
                inC.read(b);
                b.flip();
                outC.write(b);
                outC.force(false);
            }
            out.close();
            in.close();
        }catch(Exception e){
          //  Log.d(TAG, ""+e.getMessage());
            bSucc = false;
        }
        return bSucc;
    }

    /** to copy file from asset folder
     *
     * @param assetManager asset manager, may not be null
     * @param strName
     * @param strTarget
     * @return boolean true, if succeed; false, if fail.
     */
    public static boolean saveAssetsFile(AssetManager assetManager, String strName, String strTarget) {
        boolean bSucc = false;
        try {
            InputStream is = assetManager.open(strName);
            if (is == null)
                return false;
            File fOut = new File(strTarget);
            FileOutputStream ofs = new FileOutputStream(fOut);
            if (is != null && ofs != null) {
                byte[] bBuf = new byte[BUF_SIZE];
                int nRead, length = BUF_SIZE;
                while((nRead=is.read(bBuf, 0, length)) > 0) {
                    ofs.write(bBuf, 0, nRead);
                }
                ofs.close();
                is.close();
                bSucc = true;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bSucc;
    }

    /** to combine small .db0, .db1, ... files into a large file
     *
     * @param assetManager asset manager, may not be null
     * @param strName name of prefix, such as dict00202.db
     * @param strTarget target file name
     * @param strSuffix optional suffix, such as ".mp3"
     * @return boolean true, if succeed; false, if fail.
     */
    public static boolean saveSmallAssetsFile(AssetManager assetManager, String strName, String strTarget, String strSuffix) {
        boolean bSucc = false;
        int nInitSize = 0;
        int nIndex = 0;
        int nTotalSize = 0;
        byte[] bBuf = null;
        File fOut = null;
        FileOutputStream ofs = null;
        try {
            String strAppend = (TextUtils.isEmpty(strSuffix) ? "" : strSuffix);
            do{
                String strInput = strName + nIndex + strAppend;
                InputStream is = assetManager.open(strInput);
                if (is == null) {
                    if (nIndex == 0)
                        return false;
                    break;	// may not fail, for 2000K/3000K/... file, etc.
                }
                if (ofs == null) {
                    fOut = new File(strTarget);
                    ofs = new FileOutputStream(fOut);
                }
                nTotalSize = 0;
                if (is != null && ofs != null) {
                    if (bBuf == null)
                        bBuf = new byte[BUF_SIZE];
                    int nRead, length = BUF_SIZE;
                    while((nRead=is.read(bBuf, 0, length)) > 0) {
                        ofs.write(bBuf, 0, nRead);
                        nTotalSize += nRead;	// record file size
                        if (nRead < length)
                            break;
                    }
                    is.close();
                    bSucc = true;
                }
                if (nInitSize == 0)
                    nInitSize = nTotalSize;
                // to read next .db1/.db2/.db3/... file
                nIndex++;
            }while(nInitSize > 0 && nTotalSize == nInitSize);
            if (ofs != null)
                ofs.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bSucc;
    }
};